package com.team.cloudsharing.controller.user;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.cloudsharing.dto.LoginResponse;
import com.team.cloudsharing.dto.Message;
import com.team.cloudsharing.dto.User;
import com.team.cloudsharing.dto.UserChangePwd;
import com.team.cloudsharing.service.user.UserService;
import com.team.cloudsharing.ultis.Constant;
import com.team.cloudsharing.ultis.Password;

@RestController
public class UserControllerImpl implements UserController{

	final static Logger logger = Logger.getLogger(UserControllerImpl.class);
	
	@Autowired
	UserService userService;
	
	@Override
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ResponseEntity<LoginResponse> login(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody User user) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("POST /login - user: " + user.toString());
			user.setPassword(Password.createPassword(user.getPassword()));
			LoginResponse loginResponse = userService.login(user);
			return new ResponseEntity<LoginResponse>(loginResponse, HttpStatus.OK);
		}
		return new ResponseEntity<LoginResponse>(new LoginResponse(), HttpStatus.BAD_REQUEST);
	}
	
	@Override
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public ResponseEntity<Message> logout(@RequestParam String macAddress) {
		logger.info("GET /logout");
		String statusLogout = userService.logout(macAddress);
		return new ResponseEntity<Message>(new Message(statusLogout), HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/loginSocial", method = RequestMethod.POST)
	public ResponseEntity<LoginResponse> loginSocial(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody User user) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("POST /loginSocical - user: " + user.toString());
			LoginResponse loginResponse = userService.loginSocial(user);
			return new ResponseEntity<LoginResponse>(loginResponse, HttpStatus.OK);
		}
		return new ResponseEntity<LoginResponse>(new LoginResponse(), HttpStatus.BAD_REQUEST);
	}	
	
	@Override
	@RequestMapping(value = "/loginWithRememberToken/{rememberToken}", method = RequestMethod.GET)
	public ResponseEntity<LoginResponse> loginWithRememberToken(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @PathVariable("rememberToken") String rememberToken){
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("GET /loginWithRememberToken - rememberToken: " + rememberToken.toString());
			LoginResponse loginResponse = userService.loginWithRememberToken(rememberToken);
			return new ResponseEntity<LoginResponse>(loginResponse, HttpStatus.OK);
		}
		return new ResponseEntity<LoginResponse>(new LoginResponse(), HttpStatus.BAD_REQUEST);
	}

	@Override
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ResponseEntity<Message> register(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody User user) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("POST /register - user: " + user.toString());
			String statusRegister = userService.register(user);
			return new ResponseEntity<Message>(new Message(statusRegister), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}
	
	@Override
	@RequestMapping(value = "/registerSocial", method = RequestMethod.POST)
	public ResponseEntity<Message> registerSocial(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody User user) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("POST /registerSocial - user: " + user.toString());
			String statusRegister = userService.registerSocial(user);
			return new ResponseEntity<Message>(new Message(statusRegister), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}

	@Override
	@RequestMapping(value = "/registerConfirm", method = RequestMethod.GET)
	public ResponseEntity<Message> registerConfirm(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestParam("userEmail") String userEmail,
			@RequestParam("activationCode") String activationCode) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("GET /registerConfirm - userEmail: " + userEmail + " - activationCode: " + activationCode);
			String statusConfirm = userService.registerConfirm(userEmail, activationCode);
			return new ResponseEntity<Message>(new Message(statusConfirm), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}

	@Override
	@RequestMapping(value = "/resendActivationLink", method = RequestMethod.GET)
	public ResponseEntity<Message> reSendActivationLink(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestParam("userEmail") String userEmail) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){	
			logger.info("GET /resendactivationlink - userEmail: " + userEmail);
			boolean statusResendActivationLink = userService.reSendActivationLink(userEmail);
			return new ResponseEntity<Message>(new Message(String.valueOf(statusResendActivationLink)), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}
	
	@Override
	@RequestMapping(value = "/forgetPassword", method = RequestMethod.GET)
	public ResponseEntity<Message> forgetPassword(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestParam("userEmail") String userEmail) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){	
			logger.info("GET /forgetpassword - userEmail: " + userEmail);
			String statusCheckForgotPassword = userService.forgetPassword(userEmail);
			return new ResponseEntity<Message>(new Message(statusCheckForgotPassword), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}
	
	@Override
	@RequestMapping(value = "/changePassword/{keyResetPass}", method = RequestMethod.POST)
	public ResponseEntity<Message> changePassword(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody UserChangePwd userChangePwd, @PathVariable("keyResetPass") String keyResetPass) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){		
			logger.info("POST /changePassword - userChangePwd: " + userChangePwd.toString());
			String statusChangePassword = userService.changePassword(userChangePwd);
			return new ResponseEntity<Message>(new Message(statusChangePassword), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}

	@Override
	@RequestMapping(value = "/checkEmailExist", method = RequestMethod.GET)
	public ResponseEntity<Message> checkUserEmailAlreadyExisted(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestParam("userEmail") String userEmail) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){		
			logger.info("GET /checkEmailExist - userEmail: " + userEmail);
			boolean isUserEmailExist = userService.checkUserEmailAlreadyExisted(userEmail);
			return new ResponseEntity<Message>(new Message(String.valueOf(isUserEmailExist)), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}
	
	@Override
	@RequestMapping(value = "/getRememberMe", method = RequestMethod.POST)
	public ResponseEntity<Message> getRememberMe(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody User user) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("POST /getRememberMe - user: " + user.toString());
			String rememberToken = userService.getRememberToken(user);
			return new ResponseEntity<Message>(new Message(String.valueOf(rememberToken)), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}


	@Override
	@RequestMapping(value = "/updateRegistrationId", method = RequestMethod.POST)
	public ResponseEntity<Message> updateRegistrationId(@RequestHeader("Cloudsharing-Token") String cloudSharingToken, @RequestBody User user) {
		if (cloudSharingToken.equals(Constant.CLOUDSHARING_TOKEN)){
			logger.info("POST /updateRegistrationId - user: " + user.toString());
			logger.info("userId = " + user.getUserId());
			logger.info("registrationId = " + user.getRegistrationId());
			userService.updateRegistrationId(user);
			return new ResponseEntity<Message>(new Message("Update Successful"), HttpStatus.OK);
		}
		return new ResponseEntity<Message>(new Message("BAD_REQUEST"), HttpStatus.BAD_REQUEST);
	}

	
	//For increase capacity, mode = true ; decrease , mode =false
	@Override
	@RequestMapping(value = "/getCapacity", method = RequestMethod.GET)
	public ResponseEntity<Message> getCapacity(@RequestParam("userEmail") String userEmail) {
		return new ResponseEntity<Message>(new Message(String.valueOf(userService.getCapacity(userEmail))), HttpStatus.OK);
	}

}
